<?php
/**
 * 系统异常类
 * @author mao.mao
 *
 */
namespace App\Exceptions;

class SystemException extends \Exception
{

    private $exceptionName;

    protected $codeList = array(
        500 => '系统异常',
    );

    public function __construct($code, $data = NULL)
    {
        $this->exceptionName = get_class($this);
        if (isset($this->codeList[$code])) {
            $message = $this->codeList[$code];
            if (is_string($data)) {
                $data = array($data);
            }
            if (is_array($data)) {
                foreach ($data as $key => $val) {
                    $message = str_replace("{{$key}}", (string)$val, $message);
                }
            }
        } else {
            $code = -1;
            $message = 'Undefined exception information';
        }
        parent::__construct($message, $code);
    }

    public function __toString()
    {
        return 'Exception[' . $this->exceptionName . ']: [' . $this->getCode() . '] [' . $this->getMessage() . '] in ' . $this->getFile() . ' on line ' . $this->getLine();
    }
}
